// pages/zpxx_fb/fangwu.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /*图片上传*/
    imgs: [],
    placeholder: '请选择',
    /*end*/
    //获取当前位置
    chooseAddress: '请点击获取当前位置',
    index2: 0,//默认显示位置
    //房屋布局
    array3: [
      '请选择房屋布局',
      '一室一厅一卫',
      '二室一厅一卫',
      '二室二厅一卫',
      '二室二厅二卫',
      '三室一厅一卫',
      '三室一厅二卫',
      '三室二厅二卫',
      '四室一厅二卫',
      '四室二厅二卫',
    ],
    index3: 0,//默认显示位置
    //装修情况
    array4: [
      '请选择房屋装修情况',
      '毛坯',
      '简装',
      '精装',
      '豪装',
    ],
    index4: 0,//默认显示位置
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    var type = options.type
    if (type) {
      var user = wx.getStorageSync("userInfo")
      if (user) {
        var city = wx.getStorageSync("city")
        that.setData({
          user: user,
          city: city,
          type: type
        })
      } else {
        wx.showToast({
          title: "请先登录",
          icon: 'none',
          duration: 2000
        })
        setTimeout(function () {
          wx.navigateTo({
            url: '/pages/user/user'
          })
        }, 2000)
      }
    } else {
      wx.showToast({
        title: "异常",
        icon: 'none',
        duration: 2000
      })
      setTimeout(function () {
        wx.navigateBack();
      }, 2000)
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onChangeAddress: function () {
    var _page = this;
    wx.chooseLocation({
      success: function (res) {
        console.log(res);
        _page.setData({
          chooseAddress: res.name + '-' + res.address,
          latitude: res.latitude,
          longitude: res.longitude
        });
      },
      fail: function (err) {
        console.log(err);
      }
    });
  },

  //房屋规格选择器：
  bindPickerChange3: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index3: e.detail.value
    })
  },
  //装修情况选择器：
  bindPickerChange4: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index4: e.detail.value
    })
  },

  //图片上传
  chooseImg: function (e) {
    let that = this;
    var imgs = that.data.imgs;
    var imgsLength = imgs.length
    wx.chooseImage({
      count: 9 - imgsLength,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var filePaths = res.tempFilePaths
        var filePathsLength = filePaths.length;
        var failNum = 0;
        for (var i = 0; i < filePathsLength; i++) {
          wx.uploadFile({
            url: app.globalData.theUrl1 + '/Samecity/Upload/uploadFile',
            filePath: filePaths[i],
            name: 'fileData',
            formData: {},
            success: (successBack) => {
              var imgs = that.data.imgs;
              var data = JSON.parse(successBack.data)
              if (data.code == 0) {
                imgs.push(data.url);
                that.setData({
                  imgs: imgs
                })
              } else {
                failNum++;
              }
            },
            fail: (failBack) => {
              failNum++;
            },
          });
        }
        wx.showToast({
          title: '总共' + filePathsLength + '张上传成功,' + failNum + '张上传失败！',
          icon: 'none'
        });
      }
    });
  },
  // 删除图片
  deleteImg: function (e) {
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    this.setData({
      imgs: imgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    var index = e.currentTarget.dataset.index;
    var imgs = this.data.imgs;
    wx.previewImage({
      current: imgs[index],
      urls: imgs
    })
  },

  fromSub(e) {
    console.log(e.detail.value)
    var that = this
    var fromData = e.detail.value
    if (fromData.select_type == '') {
      wx.showToast({
        title: '请选择发布身份',
        icon: 'none'
      })
    } else if (fromData.location == '' || fromData.location == '请点击获取当前位置') {
      wx.showToast({
        title: '请选择房屋位置',
        icon: 'none'
      })
    }
    else if (fromData.layout == '' || fromData.layout == '请选择房屋布局') {
      wx.showToast({
        title: '请选择房屋布局',
        icon: 'none'
      })
    } else if (fromData.money == '') {
      wx.showToast({
        title: '请填写金额',
        icon: 'none'
      })
    } else if (fromData.decoration == '' || fromData.decoration == '请选择房屋装修情况') {
      wx.showToast({
        title: '请选择房屋装修情况',
        icon: 'none'
      })
    } else if (fromData.explain == '') {
      wx.showToast({
        title: '请填写备注说明',
        icon: 'none'
      })
    } else if (that.data.imgs.length == 0) {
      wx.showToast({
        title: '请上传图片',
        icon: 'none'
      })
    } else if (fromData.name == '') {
      wx.showToast({
        title: '请填写联系人姓名',
        icon: 'none'
      })
    } else if (fromData.tel == '') {
      wx.showToast({
        title: '请填写联系人电话',
        icon: 'none'
      })
    } else {
      fromData.images = that.data.imgs.join(',')
      fromData.city = that.data.city
      fromData.uid = that.data.user.id
      fromData.type = that.data.type
      app.wxRequest('post','/Samecity/House/house',fromData,(res)=>{
        if (res.data.code == 1) {
          wx.showToast({
            title: "发布成功",
            icon: 'none',
            duration: 2000
          })
          setTimeout(function () {
            wx.navigateBack();
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
        }
      },(err)=>{
        console.log(err)
      })
    }
  },
})